<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Pytest执行用例的多种方式 | 奉孝的测开日记</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  
  
    <link rel="alternate" href="/blog/atom.xml" title="奉孝的测开日记" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog/css/style.css">

  
    
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="/blog/localshare/css/share.css">

  
  
  
<meta name="generator" content="Hexo 5.4.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">奉孝的测开日记</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/blog/" id="subtitle">开启自我改变的原动力</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/blog/."><i class="fa fa-home"></i> 首页</a>
        
          <a class="main-nav-link" href="/blog/archives/"><i class="fa fa-archive"></i> 归档</a>
        
          <a class="main-nav-link" href="/blog/about/"><i class="fa fa-user"></i> 关于</a>
        
      </nav>
    </div>
    <div id="search-form">
      <div id="result-mask" class="hide"></div>
      <label><input id="search-key" type="text" autocomplete="off" placeholder="搜索"></label>
      <div id="result-wrap" class="hide">
        <div id="search-result"></div>
      </div>
      <div class="hide">
        <template id="search-tpl">
          <div class="item">
            <a href="/{path}" title="{title}">
              <div class="title">{title}</div>
              <div class="time">{date}</div>
              <div class="tags">{tags}</div>
            </a>
          </div>
        </template>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Pytest执行用例的多种方式" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Pytest执行用例的多种方式
    </h1>
  


      </header>
    
    <div class="article-meta">
      
      <span class="article-date">
  <i class="fa fa-date"></i>
  <time class="dt-published" datetime="2023-02-21T05:51:09.000Z" itemprop="datePublished">2023年02月21日</time>
</span>
      
  <div class="article-category">
    <i class="fa fa-classify"></i>
    <a class="article-category-link" href="/blog/categories/%E6%B5%8B%E8%AF%95/">测试</a>
  </div>

      
        <span class="article-views">
  <i class="fa fa-views"></i>
  <i id="busuanzi_container_page_pv">
      <i id="busuanzi_value_page_pv"></i>
  </i>
</span>

      
      
<a href="/blog/2023/02/21/Pytest%E6%89%A7%E8%A1%8C%E7%94%A8%E4%BE%8B%E7%9A%84%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F/#comments" class="article-comment-link">
  
    
    
    
    
    
  
  <i class="fa fa-commt"></i>
  留言
</a>


    </div>
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="一-、多种运行方式"><a href="#一-、多种运行方式" class="headerlink" title="一 、多种运行方式"></a>一 、多种运行方式</h1><h2 id="1-1-IDE运行"><a href="#1-1-IDE运行" class="headerlink" title="1.1 IDE运行"></a>1.1 IDE运行</h2><p>1、Pycharm运行方式配置，Tools-&gt;Python Integrated Tools-&gt;Testing 选择pytest<br>2、点击用例前面的绿色箭头运行</p>
<h2 id="1-2-命令行运行"><a href="#1-2-命令行运行" class="headerlink" title="1.2 命令行运行"></a>1.2 命令行运行</h2><p><strong>语法</strong>  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pytest 【运行参数】 【指定用例】</span><br><span class="line">#如pytest -s -v test_demo.py</span><br></pre></td></tr></table></figure>
<p><strong>运行参数</strong>  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">pytest -s test_demo.py                                         #显示代码中的print或日志打印</span><br><span class="line">pytest -v test_demo.py                                         #显示用例详情</span><br><span class="line">pytest -q test_demo.py                                         #简化显示，与-v相反</span><br><span class="line">pytest --collect-only  test_demo.py                            #检查哪些测试用例会被运行</span><br><span class="line">pytest -k &quot;not 02&quot; test_demo.py                                #选择用例运行,可以用not,or</span><br><span class="line">pytest -x test_demo.py                                         #遇到失败用例自动停止运行</span><br><span class="line">pytest --maxfail=2 test_demo.py                                #达到最大失败数自动停止运行</span><br><span class="line">pytest --tb=short test_demo.py                                 #失败信息的显示方式,三个固定值short : 仅输出assert 的一行以及系统判定的内容；line : 只使用一行输出显示所有的错误信息；no : 屏蔽所有的回溯信息</span><br><span class="line">pytest -m &quot;markname&quot; test_demo.py                              #指定标签名运行，配合mark标签装饰器</span><br><span class="line">pytest -n 3 test_demo.py                                       #分布式运行，指定3个进程，配合前面讲的xdist插件</span><br><span class="line">pytest --reruns 3 --reruns-delay 1 test_demo.py                #失败重复运行3次，每次1s间隔，配合pytest-rerunfailures插件</span><br><span class="line">pytest --html=report.html --self-contained-html test_demo.py   #输出测试报告，配合pytest-html插件</span><br><span class="line">pytest --alluredir ./result/ test_demo.py                      #输出allure报告数据到result目录，配合allure-pytest插件</span><br><span class="line">```  </span><br><span class="line">**指定用例**  </span><br></pre></td></tr></table></figure>
<p>pytest -s testsuit/                                            #指定文件夹下所有用例运行<br>pytest -s testsuit/test_demo.py                                #指定测试文件所有用例运行<br>pytest -s testsuit/test_demo.py::TestCase                      #指定测试类的所有用例运行，文件内用::连接<br>pytest -s testsuit/test_demo.py::TestCase::test_case_01        #指定单个测试用的所有用例运行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">## 1.3 main函数运行</span><br><span class="line">**语法**  </span><br></pre></td></tr></table></figure>
<p>pytest.main(args=None, plugins=None)  #传参数列表，如[‘-s’,’test_demo.py’]</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">**参数**  </span><br><span class="line">* args: 运行测试的一些参数，和命令行运行时的参数相同，在列表 List 里以字符串 str 的形式，多参数以 “,” 隔开，也可以传入测试case的路径</span><br><span class="line">* plugins: 插件参数，也是列表形式，是运行 pytest 时运行下载的插件的参数。</span><br><span class="line"></span><br><span class="line">## 1.4 通过pytest.ini运行</span><br><span class="line">**示例**  </span><br></pre></td></tr></table></figure>
<p>[pytest]<br>addopts = “-vs”   #加不加引号都可以<br>testpaths = “./test_case”<br>python_files = “test*.py”<br>python_classes = “Test”<br>python_functions = “test”<br>markers =<br>    smoke:冒烟测试用例<br>    usermanage:用户管理模块测试用例</p>
<p>```  </p>
<ul>
<li><p>addopts<br>作用：主要用来配置运行用例时，需要的参数<br>示例：pytest -vs。我们可以把执行参数写到pytest.ini里面这样写：addopts = -vs</p>
</li>
<li><p>testpaths<br>作用：配置默认读取执行的项目路径<br>示例：pytest ./test_case。我们可以把执行参数写到pytest.ini里面这样写：testpaths = ./test_case</p>
</li>
<li><p>python_files<br>作用：模块的命名规则，pytest默认查找的模块是以test开头或结尾的。可以更改此规则。<br>示例：python_files = qqq*.py。默认搜索以qqq开头的模块</p>
</li>
<li><p>python_classes<br>作用：类的命名规则，pytest默认查找的类是以Test开头的。可以更改此规则。<br>示例：python_class = qqq。默认搜索以qqq开头的类</p>
</li>
<li><p>python_functions<br>作用：方法的命名规则，pytest默认查找的方法是以test开头的。可以更改此规则。<br>示例：python_functions = qqq。默认搜索以qqq开头的方法</p>
</li>
<li><p>markers<br>作用：用例分组管理，贴标签。<br>在pytest.ini里面配置markers。然后测试类或测试用例前使用装饰器@pytest.mark.smoke，执行时使用pytest -m “smoke” ，即可执行特定标签的用例啦<br>查看标记：pytest –markers</p>
</li>
<li><p>norecursedirs<br>作用：搜索时忽略目录。norecurse的默认设置是  .*  build  dist CVS -darcs {arch} 和 *.egg。因为有  .*，所以将虚拟环境命名为.venv是一个好注意，所有以.(点)开头的目录都不会被访问。如果不是以.(点)开头，那么需要把它加入norecursedirs里。</p>
</li>
</ul>
<p>示例：比如我想忽略page目录，norecursedirs = .*  build  dist CVS -darcs {arch} *.egg page</p>
<ul>
<li><p>xfail_strict<br>作用：设置xfail_strict=True将会使那些被标记为@pytest.mark.xfail但实际通过的测试用例也被报告为失败</p>
</li>
<li><p>minversion<br>作用：指定运行测试用例的pytest的最低版本。<br>示例：minversion = 4.0。当pytest版本小于此时4.0时，会报错</p>
</li>
<li><p>cache_dir<br>作用：设置cache目录, 默认.pytest_cache<br>示例：cache_dir = ./cache</p>
</li>
<li><p>pythonpath<br>作用：添加路径到sys.path</p>
</li>
<li><p>required_plugins<br>作用：设置哪些插件在运行pytest前必须安装好。</p>
</li>
</ul>

        
            <div id="toc-article">
                
  <div class="widget-wrap" id="toc-wrap">
    <h3 class="widget-title"><i class="fa fa-toc"></i> 文章目录</h3>
    <div class="widget">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80-%E3%80%81%E5%A4%9A%E7%A7%8D%E8%BF%90%E8%A1%8C%E6%96%B9%E5%BC%8F"><span class="toc-text">一 、多种运行方式</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-IDE%E8%BF%90%E8%A1%8C"><span class="toc-text">1.1 IDE运行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%BF%90%E8%A1%8C"><span class="toc-text">1.2 命令行运行</span></a></li></ol></li></ol>
    </div>
  </div>


            </div>
        
        
          <blockquote id="copyright">
              <p>原文链接: <a href="http://haohongbin.gitee.io/blog/2023/02/21/Pytest执行用例的多种方式/">http://haohongbin.gitee.io/blog/2023/02/21/Pytest执行用例的多种方式/</a></p>
              <p>版权声明: 转载请注明出处.</p>
          </blockquote>
        
      
    </div>
    <footer class="article-footer">
      
        <div class="article-tag-wrap">
          

          
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/" rel="tag">自动化</a></li></ul>

          
    <div class="social-share">
      <span>分享到:</span>
    </div>



        </div>
      
      
        
<nav id="article-nav">
  
    <a href="/blog/2023/02/20/SQL%E6%9F%A5%E8%AF%A2%E5%9C%BA%E6%99%AF/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">older</strong>
      <div class="article-nav-title">
        
          SQL查询场景
        
      </div>
    </a>
  
  
    <a href="/blog/2023/03/09/Java%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95Mock%E7%A5%9E%E5%99%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">newer</strong>
      <div class="article-nav-title">
        
          Java单元测试Mock神器
        
      </div>
    </a>
  
</nav>

      
      
        








      
    </footer>
  </div>
</article>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-posts"></i> 最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2023/03/14/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="/blog/2023/03/09/Java%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95Mock%E7%A5%9E%E5%99%A8/">Java单元测试Mock神器</a>
          </li>
        
          <li>
            <a href="/blog/2023/02/21/Pytest%E6%89%A7%E8%A1%8C%E7%94%A8%E4%BE%8B%E7%9A%84%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F/">Pytest执行用例的多种方式</a>
          </li>
        
          <li>
            <a href="/blog/2023/02/20/SQL%E6%9F%A5%E8%AF%A2%E5%9C%BA%E6%99%AF/">SQL查询场景</a>
          </li>
        
          <li>
            <a href="/blog/2023/01/18/MySQL%E5%91%BD%E4%BB%A4%E6%89%8B%E5%86%8C/">MySQL命令手册</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签云</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/MySQL/" style="font-size: 15px;">MySQL</a> <a href="/blog/tags/%E5%88%86%E4%BA%AB/" style="font-size: 15px;">分享</a> <a href="/blog/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" style="font-size: 10px;">单元测试</a> <a href="/blog/tags/%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/" style="font-size: 20px;">学习总结</a> <a href="/blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/" style="font-size: 10px;">自动化</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-classify"></i> 分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java/">Java</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF/">Java核心技术</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/hexo%E5%8D%9A%E5%AE%A2/">hexo博客</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/vim/">vim</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E5%90%8E%E7%AB%AF/">后端</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B5%8B%E8%AF%95/">测试</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B5%8B%E8%AF%95%E8%BF%9B%E9%98%B6/">测试进阶</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-archive"></i> 归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/">2023年</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/">2022年</a><span class="archive-list-count">10</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/MySQL/" rel="tag">MySQL</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%88%86%E4%BA%AB/" rel="tag">分享</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" rel="tag">单元测试</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/" rel="tag">学习总结</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/" rel="tag">自动化</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


  
    

  
</aside>
        
      </div>
      <a id="totop" href="#top"></a>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      <p>
        <a href="/blog/sitemap.xml">网站地图</a>
        <span> | </span><a href="/blog/atom.xml">订阅本站</a>
        <span> | </span><a href="/blog/about/">联系博主</a>
      </p>
      
        <p>
          <i class="fa fa-visitors"></i>
          <i id="busuanzi_container_site_uv"><i id="busuanzi_value_site_uv"></i></i>
          ，
          <i class="fa fa-views"></i>
          <i id="busuanzi_container_site_pv"><i id="busuanzi_value_site_pv"></i></i>
        </p>
      
      <p>
        <span>Copyright &copy; 2023 奉孝.</span>
        <span>Theme by <a href="https://github.com/chaooo/hexo-theme-BlueLake/" target="_blank">BlueLake.</a></span>
        <span>Powered by <a href="https://hexo.io/" target="_blank">Hexo.</a></span>
      </p>
    </div>
  </div>
</footer>

    </div>
  </div>
  
<script src="/blog/js/jquery-3.4.1.min.js"></script>


<script src="/blog/js/search.json.js"></script>


  
<script src="/blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog/js/script.js"></script>






  
<script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  
    
<script src="/blog/localshare/js/social-share.js"></script>

    
<script src="/blog/localshare/js/qrcode.js"></script>

  
  



  

  

  

  

  

  

  

  
  





</body>
</html>